package com.taxis.service;

import java.util.List;
import org.hibernate.*;
import com.taxis.common.*;
import com.taxis.model.Comentarios;

public class ComentariosDaoService {

	private List<Comentarios> comentarios;
	private Comentarios comentario;

	public ComentariosDaoService(){ }

	public void insertComentarios(Comentarios c) throws Exception{
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try{
			tx = session.beginTransaction();
			session.save(c);
			tx.commit();
		}catch(RuntimeException re){
			if(c != null){
				tx.rollback();
			}
			throw re;
		}finally{
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Comentarios> getAllComentarios() {
		Session session = HibernateUtil.currentSession();
		try {
			Query q = session.getNamedQuery("getAllComentarios");
			comentarios = (List<Comentarios>)q.list();
			return comentarios;
		} catch(Exception e) {
			System.out.println("Error while fetching COMENTARIOS");
			e.printStackTrace();
			return null;
		} finally {
			session.close();        	
		}
	}

	public Comentarios getComentarios(Integer idComentarios) throws Exception {
		Session session = HibernateUtil.currentSession();
		try {
			return (Comentarios)session.get(Comentarios.class, idComentarios);
		} finally {
			session.close();
		}
	}

	public void updateComentarios(Comentarios c) throws Exception {
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(c);
			tx.commit();
		} catch(RuntimeException re) {
			if(c != null)
				tx.rollback();
			throw re;
		} finally {
			session.close();
		}
	}

	public void deleteComentarios(Integer idComentarios) throws Exception {
		Session session = HibernateUtil.currentSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			comentario = (Comentarios)session.load(Comentarios.class, idComentarios);
			session.delete(comentario);
			tx.commit();
		} catch (RuntimeException e) {
			if(comentario != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
}